/*--------------------------------------------------------------------------
Load raw data from Latinobar�metro. Create working sample.
Last modification: 05/03/2021
Uses:	           latinobarometro.dta
Produces:          Figures
--------------------------------------------------------------------------*/

global root "C:\Users\USERNAME\"
global code "${root}\code"
global data "${root}\data"
global res  "${root}\results"

* Intensity fairness
use "${data}\merged_data", replace
collapse (mean) very_fair unfair very_unfair fair, by(ano)
keep if ano <= 2015

#delimit ;
twoway  connect unfair      ano, lcolor(navy)         mcolor(navy)         msymbol(o) lpattern(solid)
	 || connect very_unfair ano, lcolor(maroon)       mcolor(maroon)       msymbol(d) lpattern(longdash)
	 || connect fair        ano, lcolor(orange)       mcolor(orange)       msymbol(t) lpattern(shortdash)
	 || connect very_fair   ano, lcolor(forest_green) mcolor(forest_green) msymbol(s) lpattern(dash)
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(0(10)60  , format(%4.0fc) angle(0) axis(1) gmax gmin) 
		ytitle("Percent of individuals",)
		text(55 2004 "Unfair"     , color(navy))
		text(35 2004 "Very unfair", color(maroon))
		text(17 2004 "Fair"       , color(orange))
		text(7  2004 "Very fair"  , color(forest_green))
		legend(off) graphregion(color(white)) bgcolor(white);
		graph export "${res}\fig-intensity-fairness.pdf", replace;


* Change unfair 2002 vs. 2013
#delimit;
use "${data}\merged_data.dta", replace;
keep pais ano unfair_all; rename unfair_all unfair_;
reshape wide unfair_, i(pais) j(ano);

replace unfair_2002 = unfair_2007 if pais == "dom";

preserve; collapse (mean) unfair*; gen pais = "lac"; tempfile lac; save `lac', replace; restore; append using `lac';

gen     pais_lab = pais;
replace pais_lab = "Argentina"     if pais == "arg";
replace pais_lab = "Bolivia"       if pais == "bol";
replace pais_lab = "Brazil"        if pais == "bra";
replace pais_lab = "Chile"         if pais == "chl";
replace pais_lab = "Colombia"      if pais == "col";
replace pais_lab = "Costa Rica"    if pais == "cri";
replace pais_lab = "Dom. R."       if pais == "dom";
replace pais_lab = "Ecuador"       if pais == "ecu";
replace pais_lab = "Guatemala"     if pais == "gtm";
replace pais_lab = "Honduras"      if pais == "hnd";
replace pais_lab = "Mexico"        if pais == "mex";
replace pais_lab = "Nicaragua"     if pais == "nic";
replace pais_lab = "Panama"        if pais == "pan";
replace pais_lab = "Peru"          if pais == "per";
replace pais_lab = "Paraguay"      if pais == "pry";
replace pais_lab = "El Salvador"   if pais == "slv";
replace pais_lab = "Uruguay"       if pais == "ury";
replace pais_lab = "Venezuela"     if pais == "ven";
replace pais_lab = "Latin America" if pais == "lac";

gen     pos = .;
replace pos = 8  if pais == "arg";
replace pos = 8  if pais == "bol";
replace pos = 7  if pais == "bra"; 
replace pos = 4  if pais == "chl"; 
replace pos = 3  if pais == "col"; 
replace pos = 9  if pais == "cri"; 
replace pos = 6  if pais == "dom"; 
replace pos = 6  if pais == "ecu"; 
replace pos = 12 if pais == "hnd"; 
replace pos = 9  if pais == "gtm"; 
replace pos = 6  if pais == "mex"; 
replace pos = 6  if pais == "nic"; 
replace pos = 6  if pais == "pan"; 
replace pos = 3  if pais == "per"; 
replace pos = 2  if pais == "pry"; 
replace pos = 6  if pais == "slv";
replace pos = 3  if pais == "ury"; 
replace pos = 9  if pais == "ven"; 
replace pos = 5  if pais == "lac"; 

twoway scatter unfair_2013 unfair_2002 if pais != "lac",  mlabel(pais_lab) mlabcolor(black) msym(O) mlabv(pos)
    || scatter unfair_2013 unfair_2002 if pais == "lac",  mlabel(pais_lab) mlabcolor(red)   msym(D) mlabv(pos)
    || lfit    unfair_2013 unfair_2013, range(30 100) lcolor(gs12) lpattern(dash) 
	   xtitle("Percent unfair or very unfair in 2002") xlabel(30(10)100, format(%4.0f)  grid) xscale(titlegap(2)) 
	   ytitle("Percent unfair or very unfair in 2013") ylabel(30(10)100, format(%4.0f)  gmax gmin angle(0))
	   xline(0, lcolor(gs12) lwidth(vthin)) 
	   yline(0, lcolor(gs12) lwidth(vthin))
	   legend(off) graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig-chg-fairness-0213.pdf", replace;



* Change gini vs. Change unfair
#delimit ;

use "${data}\raw\inequality_indicators_original.dta", clear;
drop if pais == "arg" & ano < 2004                          ;
drop if pais == "col" & inlist(ano,1999,2000,2001,2006,2007);
drop if pais == "cri" & ano < 2010                          ;
drop if pais == "dom" & ano == 2010                         ;
drop if pais == "ecu" & inlist(ano,1998,1999,2000)          ;
drop if pais == "gtm" & inlist(ano,2002,2003,2004)          ;
drop if pais == "pan" & ano < 2008                          ;
drop if pais == "per" & ano < 2004                          ;

keep if indicator == "Gini coefficient"; drop indicator ;
rename value gini_ ;
keep if ano >= 2000;
reshape wide gini, i(pais) j(ano);
rename gini* ginilel*;
tempfile gini; save `gini', replace;

use pais ano unfair_all using "${data}\merged_data.dta", replace;
rename unfair_all unfair_ ;
reshape wide unfair_, i(pais) j(ano);

merge 1:1 pais using `gini', nogen;

gen gini_ini   = .;
gen gini_fin   = .;
gen unfair_ini = .;
gen unfair_fin = .;
gen chg_gini   = .;
gen chg_unfair = .;

replace gini_ini = ginilel_2002                  ;
replace gini_ini = ginilel_2004  if pais == "arg";
replace gini_ini = ginilel_2003  if pais == "chl";
replace gini_ini = ginilel_2010  if pais == "cri";
replace gini_ini = ginilel_2003  if pais == "ecu";
replace gini_ini = ginilel_2006  if pais == "gtm";
replace gini_ini = ginilel_2001  if pais == "nic";
replace gini_ini = ginilel_2001  if pais == "nic";
replace gini_ini = ginilel_2008  if pais == "pan";
replace gini_ini = ginilel_2004  if pais == "per";
replace gini_fin = ginilel_2013                  ;
replace gini_fin = ginilel_2014  if pais == "gtm";
replace gini_fin = ginilel_2014  if pais == "mex";
replace gini_fin = ginilel_2014  if pais == "nic";
replace gini_fin = ginilel_2012  if pais == "ven";
												 
replace unfair_ini = unfair_2002                 ;
replace unfair_ini = unfair_2007 if pais == "ecu";
replace unfair_ini = unfair_2007 if pais == "dom";
replace unfair_fin = unfair_2013                 ;

replace chg_gini   = gini_fin    - gini_ini      ;
replace chg_unfair = unfair_fin  - unfair_ini    ;

preserve; collapse (mean) chg*; gen pais = "lac"; tempfile lac; save `lac', replace; restore; append using `lac';

gen     pais_lab = pais;
replace pais_lab = "Argentina"     if pais == "arg";
replace pais_lab = "Bolivia"       if pais == "bol";
replace pais_lab = "Brazil"        if pais == "bra";
replace pais_lab = "Chile"         if pais == "chl";
replace pais_lab = "Colombia"      if pais == "col";
replace pais_lab = "Costa Rica"    if pais == "cri";
replace pais_lab = "Dom. R."       if pais == "dom";
replace pais_lab = "Ecuador"       if pais == "ecu";
replace pais_lab = "Guatemala"     if pais == "gtm";
replace pais_lab = "Honduras"      if pais == "hnd";
replace pais_lab = "Mexico"        if pais == "mex";
replace pais_lab = "Nicaragua"     if pais == "nic";
replace pais_lab = "Panama"        if pais == "pan";
replace pais_lab = "Peru"          if pais == "per";
replace pais_lab = "Paraguay"      if pais == "pry";
replace pais_lab = "El Salvador"   if pais == "slv";
replace pais_lab = "Uruguay"       if pais == "ury";
replace pais_lab = "Venezuela"     if pais == "ven";
replace pais_lab = "Latin America" if pais == "lac";

gen     pos = .                  ;
replace pos = 7  if pais == "arg"; 
replace pos = 8  if pais == "bol";
replace pos = 6  if pais == "bra"; 
replace pos = 9  if pais == "chl"; 
replace pos = 5  if pais == "col"; 
replace pos = 3  if pais == "cri"; 
replace pos = 5  if pais == "dom"; 
replace pos = 3  if pais == "ecu"; 
replace pos = 2  if pais == "gtm"; 
replace pos = 6  if pais == "hnd"; 
replace pos = 6  if pais == "mex"; 
replace pos = 3  if pais == "nic"; 
replace pos = 7  if pais == "pan"; 
replace pos = 9  if pais == "per"; 
replace pos = 9  if pais == "pry"; 
replace pos = 9  if pais == "slv";
replace pos = 7  if pais == "ury"; 
replace pos = 9  if pais == "ven"; 
replace pos = 6  if pais == "lac"; 

#delimit;

twoway scatter chg_unfair chg_gini if pais != "lac", mlabel(pais_lab) mlabcolor(black) msym(O) mlabv(pos) mlabgap(*1.1)
    || scatter chg_unfair chg_gini if pais == "lac", mlabel(pais_lab) mlabcolor(red) msym(d) mlabv(pos) mlabgap(*1.1)
	   xtitle("Change in Gini coefficient (2002-2013)") xlabel(-.15(.05).15, format(%4.2f)  grid) xscale(titlegap(2)) 
	   ytitle("Change in share unfair (2002-2013)")     ylabel(-40(10)10   , format(%4.0f)  gmax gmin angle(0))
	   xline(0, lcolor(gs12) lwidth(vthin)) 
	   yline(0, lcolor(gs12) lwidth(vthin))
	   legend(off) graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig-chg-gini-unfair-0213.pdf", replace;




* Binned scatterplot Unfair vs. Gini
#delimit; 
use "${data}\merged_data", replace;
keep pais ano gini* gini_lel_original gini_abs_original unfair_all unfair very_unfair;

foreach v in gini_lel unfair_all {;
	qui sum `v' if ano <= 2015, ;
	gen `v'_std = (`v'- r(mean))/r(sd);
};

reg      gini_lel_std unfair_all_std if ano <= 2015;
spearman gini_lel_std unfair_all_std if ano <= 2015;
corr gini gini_lel       if ano <= 2014;

twoway scatter unfair_all gini_lel, mcolor(gs12) msym(O) lpattern(solid)
	|| lfit    unfair_all gini_lel, lcolor(maroon) lpattern(dash)
	   ytitle("% Unfair or very unfair") ylabel(, angle(0) format(%4.0f))
	   xtitle("Gini coefficient")        xlabel(, format(%4.2f))   
	   legend(off) graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig-scatter-gini-unfair.pdf", replace;


gen bin = floor(gini_lel*50)/50; gen n = 1;
collapse (mean) unfair* very* (sum) n, by(bin);

keep if n >= 3; 

#delimit;
corr unfair_all bin [w=n]; reg unfair_all bin  [w=n];
twoway scatter unfair_all bin [fw=n], mcolor(gs12) msym(O) lpattern(solid)
	|| lfit    unfair_all bin [fw=n], lcolor(maroon) lpattern(dash)
	   ytitle("Percent unfair or very unfair") ylabel(50(10)100, angle(0) format(%4.0f) gmin gmax)
	   xtitle("Gini coefficient")        xlabel(, format(%4.2f))
	   legend(off) graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig-binscatter-gini-unfair.pdf", replace;


 
#delimit;

qui corr unfair      bin [fw=n]; glo unfair      : display %4.2f r(rho);
qui corr very_unfair bin [fw=n]; glo very_unfair : display %4.2f r(rho);
qui corr unfair_all  bin [fw=n]; glo unfair_all  : display %4.2f r(rho);

twoway scatter unfair      bin, mcolor(navy)         msym(O) lpattern(solid)
	|| scatter very_unfair bin, mcolor(maroon)       msym(T) lpattern(solid)
	|| scatter unfair_all  bin, mcolor(forest_green) msym(D) lpattern(solid)
	|| lfit    unfair      bin, lcolor(navy)         lpattern(solid)
	|| lfit    very_unfair bin, lcolor(maroon)       lpattern(solid)
	|| lfit    unfair_all  bin, lcolor(forest_green) lpattern(solid)
	   ytitle("Percent of individuals") ylabel(0(20)100, angle(0) format(%4.0f) gmin gmax)
	   xtitle("Gini coefficient")        xlabel(, format(%4.2f))
	   legend(order(1 "Unfair (Correlation = ${unfair})" 2 "Very unfair (Correlation = ${very_unfair})" 3 "Unfair or very unfair (Correlation = ${unfair_all})") col(1) pos(5) ring(0)) 
	   graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig-binscatter-gini-unfair-all.pdf", replace;







* Time series Unfair and Gini 
#delimit;
use "${data}\merged_data", replace;

collapse (mean) gini gini_lel gini_abs_original unfair_all unfair very_unfair, by(ano);
drop if ano > 2015;

foreach v in gini_lel unfair_all {;
	qui sum `v' if ano <= 2015, ;
	gen `v'_std = (`v'- r(mean))/r(sd);
};

reg gini_lel_std unfair_all_std if ano <= 2015;

twoway  connect unfair_all ano, lcolor(navy)   mcolor(navy)   msymbol(o)
	 || connect gini_lel   ano, lcolor(maroon) mcolor(maroon) msymbol(d) lpattern(longdash) yaxis(2) 
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(60(10)100  , format(%4.0fc) angle(0) axis(1) gmax gmin) ytitle("",)
		ylabel(0.4(0.05).6, format(%4.2fc) angle(0) axis(2) gmax gmin) ytitle("", axis(2)) 
		text(75 2004 "%	Unfair or very unfair (LHS)", color(navy))
		text(85 2008.3 "Gini coefficient (RHS)", color(maroon))
		legend(off) graphregion(color(white)) bgcolor(white);
		graph export "${res}\fig-timeseries-gini-unfair.pdf", replace;



* Pooled Scatterplot Unfair vs. Absolute Gini 
#delimit;
use "${data}\merged_data", replace;
keep pais ano gini_lel gini_lel_original gini_abs_original unfair_all unfair very_unfair;

#delimit;
twoway scatter unfair_all gini_abs, mcolor(gs12) msym(O) lpattern(solid)
	|| lfit    unfair_all gini_abs, lcolor(maroon) lpattern(dash)
	   ytitle("% Unfair or very unfair") ylabel(, angle(0) format(%4.0f))
	   xtitle("Absolute Gini (index)")   xlabel(, format(%4.0f))   
	   legend(off) graphregion(color(white)) bgcolor(white);
	   graph export "${res}\fig-chg-absgini-unfair.pdf", replace;



* Time series Unfair and Gini 
#delimit;
use "${data}\merged_data", replace;
keep pais ano gini_abs unfair_all unfair very_unfair;
collapse (mean) gini_abs unfair_all unfair very_unfair, by(ano);
drop if year > 2015;

#delimit ;
twoway  connect unfair_all ano, lcolor(navy)   mcolor(navy)   msymbol(o)
	 || connect gini_abs   ano, lcolor(maroon) mcolor(maroon) msymbol(d) lpattern(longdash) yaxis(2) 
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(60(10)100, format(%4.0fc) angle(0) axis(1) gmax gmin) ytitle("",)
		ylabel(60(10)120, format(%4.0fc) angle(0) axis(2) gmax gmin) ytitle("", axis(2)) 
		text(75 2004 "% Unfair or very unfair (LHS)", color(navy))
		text(90 2007.5 "Absolute Gini index (RHS)", color(maroon))
		legend(off) graphregion(color(white)) bgcolor(white);
	


* Time series Unfair by group

#delimit;
use "${data}\fairness_groups", replace; rename percent unfair;
keep if inlist(class,"Unfair","Very unfair");
drop if year > 2015;
collapse (sum) unfair, by(category level year);
		

#delimit ;
twoway  connect unfair year if level == "15-24", lcolor(navy)         mcolor(navy)         msymbol(o) lpattern(solid)
     || connect unfair year if level == "25-40", lcolor(maroon)       mcolor(maroon)       msymbol(d) lpattern(longdash)
     || connect unfair year if level == "41-64", lcolor(orange)       mcolor(orange)       msymbol(t) lpattern(shortdash)
     || connect unfair year if level == "65+"  , lcolor(forest_green) mcolor(forest_green) msymbol(s) lpattern(dash)
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(60(10)100, format(%4.0fc) angle(0) axis(1) gmax gmin) ytitle("",)
		legend(order(1 "15-24" 2 "25-40" 3 "41-64" 4 "65+") row(1) size(small)) 
		graphregion(color(white)) bgcolor(white);
	    graph export "${res}\fig-fairness-age.pdf", replace;


#delimit ;
twoway  connect unfair year if level == "Female", lcolor(maroon)       mcolor(maroon)       msymbol(d) lpattern(longdash)
     || connect unfair year if level == "Male", lcolor(navy)         mcolor(navy)         msymbol(o) lpattern(solid)
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(60(10)100, format(%4.0fc) angle(0) axis(1) gmax gmin) ytitle("",)
		legend(order(1 "Female" 2 "Male") row(1) size(small)) 
		graphregion(color(white)) bgcolor(white);
	    graph export "${res}\fig-fairness-sex.pdf", replace;


#delimit ;
twoway  connect unfair year if level == "Less than Primary", lcolor(navy)         mcolor(navy)         msymbol(o) lpattern(solid)
     || connect unfair year if level == "Complete Primary", lcolor(maroon)       mcolor(maroon)       msymbol(d) lpattern(longdash)
     || connect unfair year if level == "Complete Secondary", lcolor(orange)       mcolor(orange)       msymbol(t) lpattern(shortdash)
     || connect unfair year if level == "Complete Tertiary"  , lcolor(forest_green) mcolor(forest_green) msymbol(s) lpattern(dash)
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(60(10)100, format(%4.0fc) angle(0) axis(1) gmax gmin) ytitle("",)
		legend(order(1 "Less than primary" 2 "Primary" 3 "Secondary" 4 "Tertiary") row(1) size(small)) 
		graphregion(color(white)) bgcolor(white);
	    graph export "${res}\fig-fairness-educ.pdf", replace;


#delimit ;
twoway  connect unfair year if level == "Employee", lcolor(navy)         mcolor(navy)         msymbol(o) lpattern(solid)
     || connect unfair year if level == "Employer", lcolor(maroon)       mcolor(maroon)       msymbol(d) lpattern(longdash)
     || connect unfair year if level == "Self-employed", lcolor(orange)       mcolor(orange)       msymbol(t) lpattern(shortdash)
     || connect unfair year if level == "Unemployed"  , lcolor(forest_green) mcolor(forest_green) msymbol(s) lpattern(dash)
		xtitle(" ") xlabel(1997(3)2015) 
		ylabel(60(10)100, format(%4.0fc) angle(0) axis(1) gmax gmin) ytitle("",)
		legend(order(1 "Employee" 2 "Employer" 3 "Self-employed" 4 "Unemployed") row(1) size(small) span) 
		graphregion(color(white)) bgcolor(white);
	    graph export "${res}\fig-fairness-employ.pdf", replace;







	

/* End of do-file */
exit
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
